package com.ymatou.iapi.livebuyer.testcase;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.livebuyer.parameter.EnterGroupBean;
import com.ymatou.iapi.livebuyer.service.EnterGroupCall;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.livevideo.parameter.StreamLiveNotifyBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.database.sqlwapper.YmtLoggingWapper;

/**
 * 进入直播间 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_EnterGroup {
	private static EnterGroupBean entergroupBean;
	private static EnterGroupCall entergroupCall;
	private static LiveRoomWapper liveRoomWapper;
	private static Integer liveid;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("进入直播间");
	}

	@Before
	public void caseUp() {
		entergroupBean = new EnterGroupBean();
		entergroupCall = new EnterGroupCall();
		liveRoomWapper = new LiveRoomWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(entergroupCall.getOpurl());
	}

	@After
	public void caseDown() {
		liveRoomWapper = null;
		Logger.end();
	}

	private void verifyDB(EnterGroupBean entergroupBean, boolean isonline, boolean isview, int bonline, int bview) {
		int liveid = entergroupBean.getLiveId();
		int uid = entergroupBean.getUserId();
		Logger.comment("验证Livemember");
		List<Map> templist = liveRoomWapper.selectLivemember(liveid, uid);
		Logger.verifyEquals(1, templist.size(), "验证有记录");
		if (templist.size() > 0) {
			Map alivemember = liveRoomWapper.selectLivemember(liveid, uid).get(0);
			Logger.verifyEquals(1, alivemember.get("Status"), "验证状态");
		}

		Logger.comment("验证liveroomstatus");

		Map liveroom = liveRoomWapper.selectLiveRoomStatById(liveid);
		int aonline = 0;
		int aview = 0;
		if (liveroom != null) {
			Logger.debug(liveroom);
			aonline = Integer.valueOf(liveroom.get("OnLineNum").toString());
			aview = Integer.valueOf(liveroom.get("ViewRealNum").toString());
		}
		if (isonline) {
			Logger.verifyEquals(bonline, aonline, "验证online");
		} else {
			// 原来不是onlie的情况 +1
			Logger.verifyEquals(bonline + 1, aonline, "验证online");
		}
		if (isview) {
			Logger.verifyEquals(bview, aview, "验证view");
		} else {
			// 原来没有观看view的情况 +1
			Logger.verifyEquals(bview + 1, aview, "验证view");
		}

	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_EnterGroup_001() {
		Logger.start(true, "进入直播间成功");
		try {
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			liveid = livedto.getId();
			Integer uid = 1;
			LiveRoomCallService.SellerLiveOnline(liveid, livedto.getUserId());
			// 删除存量记录
			liveRoomWapper.deleteLivemember(liveid, uid);
			LiveRoomCallService.PublishLiveStatCall(null);
			// 查一下当前的在线数和观看数
			Map liveroom = liveRoomWapper.selectLiveRoomStatById(liveid);
			int bonline = 0;
			int bview = 0;
			if (liveroom != null) {
				Logger.debug(liveroom);
				bonline = Integer.valueOf(liveroom.get("OnLineNum").toString());
				bview = Integer.valueOf(liveroom.get("ViewRealNum").toString());
			}
			// 是否在线
			boolean isonline = false;
			// 是否观看
			boolean isview = false;
			List<Map> templist = liveRoomWapper.selectLivemember(liveid, uid);
			Map blivemember = null;
			if (templist.size() > 0) {
				blivemember = liveRoomWapper.selectLivemember(liveid, uid).get(0);
				Logger.debug(blivemember);
				isview = true;
				if (blivemember.get("Status").equals(1)) {
					isonline = true;
				}
			}
			entergroupBean.setLiveId(liveid);
			entergroupBean.setUserId(uid);
			entergroupCall.setData(entergroupBean);
			entergroupCall.callService();
			Logger.verifyEquals("200", entergroupCall.getString("code"), "验证code");
			LiveRoomCallService.PublishLiveStatCall(liveid);
			verifyDB(entergroupBean, isonline, isview, bonline, bview);
			Logger.comment("验证日志数据");
			Map m = new YmtLoggingWapper().selectLiveBehaviorById(liveid, uid);
			Logger.debug(m);
			Logger.verifyEquals(1, m.get("BehaviorType"), "验证BehaviorType");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_EnterGroup_002() {
		Logger.start(true, "不输入liveid失败");
		try {
			// entergroupBean.setLiveId(liveid);
			entergroupBean.setUserId(1);
			entergroupCall.setData(entergroupBean);
			entergroupCall.callService();
			Logger.verifyEquals("100", entergroupCall.getString("code"), "验证code");
			Logger.verifyIsContains("无效的直播编号", entergroupCall.getString("message"), "验证message");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_EnterGroup_003() {
		Logger.start(true, "不输入userid失败");
		try {
			entergroupBean.setLiveId(1);
			// entergroupBean.setUserId(1);
			entergroupCall.setData(entergroupBean);
			entergroupCall.callService();
			Logger.verifyEquals("100", entergroupCall.getString("code"), "验证code");
			Logger.verifyIsContains("无效的用户编号", entergroupCall.getString("message"), "验证message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_EnterGroup_004() {
		Logger.start(true, "输入不存在liveid失败");
		try {
			entergroupBean.setLiveId(-1);
			entergroupBean.setUserId(1);
			entergroupCall.setData(entergroupBean);
			entergroupCall.callService();
			Logger.verifyEquals("100", entergroupCall.getString("code"), "验证code");
			Logger.verifyIsContains("无效的直播编号", entergroupCall.getString("message"), "验证message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_EnterGroup_005() {
		Logger.start(true, "重复进入直播间成功");
		try {
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			liveid = livedto.getId();
			Integer uid = 1;
			LiveRoomCallService.SellerLiveOnline(liveid, livedto.getUserId());
			// 删除存量记录
			liveRoomWapper.deleteLivemember(liveid, uid);
			LiveRoomCallService.PublishLiveStatCall(liveid);
			// 查一下当前的在线数和观看数
			Map liveroom = liveRoomWapper.selectLiveRoomStatById(liveid);
			int bonline = 0;
			int bview = 0;
			if (liveroom != null) {
				Logger.debug(liveroom);
				bonline = Integer.valueOf(liveroom.get("OnLineNum").toString());
				bview = Integer.valueOf(liveroom.get("ViewRealNum").toString());
			}
			// 是否在线
			boolean isonline = false;
			// 是否观看
			boolean isview = false;
			List<Map> templist = liveRoomWapper.selectLivemember(liveid, uid);
			Map blivemember = null;
			if (templist.size() > 0) {
				blivemember = liveRoomWapper.selectLivemember(liveid, uid).get(0);
				Logger.debug(blivemember);
				isview = true;
				if (blivemember.get("Status").equals(1)) {
					isonline = true;
				}
			}
			entergroupBean.setLiveId(liveid);
			entergroupBean.setUserId(uid);
			entergroupCall.setData(entergroupBean);
			entergroupCall.callService();
			entergroupCall.callService();
			Logger.verifyEquals("200", entergroupCall.getString("code"), "验证code");
			LiveRoomCallService.PublishLiveStatCall(liveid);
			verifyDB(entergroupBean, isonline, isview, bonline, bview);
			//TODO 验证mssql  logging  select * from LiveBehavior where liveid=5032 记录多条
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_EnterGroup_006() {
		Logger.start(true, "退出再进入直播间成功");
		try {
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			liveid = livedto.getId();
			Integer uid = 1;
			// 删除存量记录
			LiveRoomCallService.SellerLiveOnline(liveid, livedto.getUserId());
			liveRoomWapper.deleteLivemember(liveid, uid);
			LiveRoomCallService.PublishLiveStatCall(liveid);
			// 查一下当前的在线数和观看数
			Map liveroom = liveRoomWapper.selectLiveRoomStatById(liveid);
			int bonline = 0;
			int bview = 0;
			if (liveroom != null) {
				Logger.debug(liveroom);
				bonline = Integer.valueOf(liveroom.get("OnLineNum").toString());
				bview = Integer.valueOf(liveroom.get("ViewRealNum").toString());
			}
			// 是否在线
			boolean isonline = false;
			// 是否观看
			boolean isview = false;
			List<Map> templist = liveRoomWapper.selectLivemember(liveid, uid);
			Map blivemember = null;
			if (templist.size() > 0) {
				blivemember = liveRoomWapper.selectLivemember(liveid, uid).get(0);
				Logger.debug(blivemember);
				isview = true;
				if (blivemember.get("Status").equals(1)) {
					isonline = true;
				}
			}
			entergroupBean.setLiveId(liveid);
			entergroupBean.setUserId(uid);
			entergroupCall.setData(entergroupBean);
			entergroupCall.callService();
			LiveRoomCallService.quitGroup(liveid, uid);
			entergroupCall.callService();
			Logger.verifyEquals("200", entergroupCall.getString("code"), "验证code");
			LiveRoomCallService.PublishLiveStatCall(liveid);
			verifyDB(entergroupBean, isonline, isview, bonline, bview);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_EnterGroup_007() {
		Logger.start(true, "直播状态7结束冻结围观人数-不计数");
		try {
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			liveid = livedto.getId();
			LiveRoomCallService.SellerLiveOnline(liveid, livedto.getUserId());
			StreamLiveNotifyBean notify = LiveRoomCallService.initStreamLiveNotifyBean(liveid, 1);
			LiveRoomCallService.LiveNotify(notify);
			Integer uid = 1;
			// 删除存量记录
			liveRoomWapper.deleteLivemember(liveid, uid);
			LiveRoomCallService.PublishLiveStatCall(liveid);
			// 查一下当前的在线数和观看数
			Map liveroom = liveRoomWapper.selectLiveRoomStatById(liveid);
			int bonline = 0;
			int bview = 0;
			if (liveroom != null) {
				Logger.debug(liveroom);
				bonline = Integer.valueOf(liveroom.get("OnLineNum").toString());
				bview = Integer.valueOf(liveroom.get("ViewRealNum").toString());
			}
			// 是否在线
			boolean isonline = false;
			// 是否观看
			boolean isview = false;
			List<Map> templist = liveRoomWapper.selectLivemember(liveid, uid);
			Map blivemember = null;
			if (templist.size() > 0) {
				blivemember = liveRoomWapper.selectLivemember(liveid, uid).get(0);
				Logger.debug(blivemember);
				isview = true;
				if (blivemember.get("Status").equals(1)) {
					isonline = true;
				}
			}

			liveRoomWapper.updateLiveRoomPushStatus(liveid, 7);
			entergroupBean.setLiveId(liveid);
			entergroupBean.setUserId(uid);
			entergroupCall.setData(entergroupBean);
			entergroupCall.callService();
			Logger.verifyEquals("200", entergroupCall.getString("code"), "验证code");
			verifyDB(entergroupBean, true, true, bonline, bview);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_EnterGroup_008() {
		Logger.start(true, "直播状态8结束冻结围观人数-不计数");
		try {
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			liveid = livedto.getId();
			LiveRoomCallService.SellerLiveOnline(liveid, livedto.getUserId());
			StreamLiveNotifyBean notify = LiveRoomCallService.initStreamLiveNotifyBean(liveid, 1);
			LiveRoomCallService.LiveNotify(notify);
			Integer uid = 1;
			// 删除存量记录
			liveRoomWapper.deleteLivemember(liveid, uid);
			LiveRoomCallService.PublishLiveStatCall(liveid);
			// 查一下当前的在线数和观看数
			Map liveroom = liveRoomWapper.selectLiveRoomStatById(liveid);
			int bonline = 0;
			int bview = 0;
			if (liveroom != null) {
				Logger.debug(liveroom);
				bonline = Integer.valueOf(liveroom.get("OnLineNum").toString());
				bview = Integer.valueOf(liveroom.get("ViewRealNum").toString());
			}
			// 是否在线
			boolean isonline = false;
			// 是否观看
			boolean isview = false;
			List<Map> templist = liveRoomWapper.selectLivemember(liveid, uid);
			Map blivemember = null;
			if (templist.size() > 0) {
				blivemember = liveRoomWapper.selectLivemember(liveid, uid).get(0);
				Logger.debug(blivemember);
				isview = true;
				if (blivemember.get("Status").equals(1)) {
					isonline = true;
				}
			}

			new LiveRoomWapper().updateLiveRoomPushStatus(liveid, 8);
			entergroupBean.setLiveId(liveid);
			entergroupBean.setUserId(uid);
			entergroupCall.setData(entergroupBean);
			entergroupCall.callService();
			Logger.verifyEquals("200", entergroupCall.getString("code"), "验证code");
			verifyDB(entergroupBean, true, true, bonline, bview);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_EnterGroup_009() {
		Logger.start(true, "直播状态9结束冻结围观人数-不计数");
		try {
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			liveid = livedto.getId();
			LiveRoomCallService.SellerLiveOnline(liveid, livedto.getUserId());
			StreamLiveNotifyBean notify = LiveRoomCallService.initStreamLiveNotifyBean(liveid, 1);
			LiveRoomCallService.LiveNotify(notify);
			Integer uid = 1;
			// 删除存量记录
			liveRoomWapper.deleteLivemember(liveid, uid);
			LiveRoomCallService.PublishLiveStatCall(liveid);
			// 查一下当前的在线数和观看数
			Map liveroom = liveRoomWapper.selectLiveRoomStatById(liveid);
			int bonline = 0;
			int bview = 0;
			if (liveroom != null) {
				Logger.debug(liveroom);
				bonline = Integer.valueOf(liveroom.get("OnLineNum").toString());
				bview = Integer.valueOf(liveroom.get("ViewRealNum").toString());
			}
			// 是否在线
			boolean isonline = false;
			// 是否观看
			boolean isview = false;
			List<Map> templist = liveRoomWapper.selectLivemember(liveid, uid);
			Map blivemember = null;
			if (templist.size() > 0) {
				blivemember = liveRoomWapper.selectLivemember(liveid, uid).get(0);
				Logger.debug(blivemember);
				isview = true;
				if (blivemember.get("Status").equals(1)) {
					isonline = true;
				}
			}

			new LiveRoomWapper().updateLiveRoomPushStatus(liveid, 9);
			entergroupBean.setLiveId(liveid);
			entergroupBean.setUserId(uid);
			entergroupCall.setData(entergroupBean);
			entergroupCall.callService();
			Logger.verifyEquals("200", entergroupCall.getString("code"), "验证code");
			verifyDB(entergroupBean, true, true, bonline, bview);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_EnterGroup_010() {
		Logger.start(true, "直播状态10结束冻结围观人数-不计数");
		try {
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			liveid = livedto.getId();
			LiveRoomCallService.SellerLiveOnline(liveid, livedto.getUserId());
			StreamLiveNotifyBean notify = LiveRoomCallService.initStreamLiveNotifyBean(liveid, 1);
			LiveRoomCallService.LiveNotify(notify);
			Integer uid = 1;
			// 删除存量记录
			liveRoomWapper.deleteLivemember(liveid, uid);
			LiveRoomCallService.PublishLiveStatCall(liveid);
			// 查一下当前的在线数和观看数
			Map liveroom = liveRoomWapper.selectLiveRoomStatById(liveid);
			int bonline = 0;
			int bview = 0;
			if (liveroom != null) {
				Logger.debug(liveroom);
				bonline = Integer.valueOf(liveroom.get("OnLineNum").toString());
				bview = Integer.valueOf(liveroom.get("ViewRealNum").toString());
			}
			// 是否在线
			boolean isonline = false;
			// 是否观看
			boolean isview = false;
			List<Map> templist = liveRoomWapper.selectLivemember(liveid, uid);
			Map blivemember = null;
			if (templist.size() > 0) {
				blivemember = liveRoomWapper.selectLivemember(liveid, uid).get(0);
				Logger.debug(blivemember);
				isview = true;
				if (blivemember.get("Status").equals(1)) {
					isonline = true;
				}
			}

			new LiveRoomWapper().updateLiveRoomPushStatus(liveid, 10);
			entergroupBean.setLiveId(liveid);
			entergroupBean.setUserId(uid);
			entergroupCall.setData(entergroupBean);
			entergroupCall.callService();
			Logger.verifyEquals("200", entergroupCall.getString("code"), "验证code");
			verifyDB(entergroupBean, true, true, bonline, bview);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_EnterGroup_011() {
		Logger.start(true, "游客进入直播间成功");
		try {
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			liveid = livedto.getId();
			Integer uid = -1;
			LiveRoomCallService.SellerLiveOnline(liveid, livedto.getUserId());
			// 删除存量记录
			liveRoomWapper.deleteLivemember(liveid, uid);
			LiveRoomCallService.PublishLiveStatCall(null);
			// 查一下当前的在线数和观看数
			Map liveroom = liveRoomWapper.selectLiveRoomStatById(liveid);
			int bonline = 0;
			int bview = 0;
			if (liveroom != null) {
				Logger.debug(liveroom);
				bonline = Integer.valueOf(liveroom.get("OnLineNum").toString());
				bview = Integer.valueOf(liveroom.get("ViewRealNum").toString());
			}
			// 是否在线
			boolean isonline = false;
			// 是否观看
			boolean isview = false;
			List<Map> templist = liveRoomWapper.selectLivemember(liveid, uid);
			Map blivemember = null;
			if (templist.size() > 0) {
				blivemember = liveRoomWapper.selectLivemember(liveid, uid).get(0);
				Logger.debug(blivemember);
				isview = true;
				if (blivemember.get("Status").equals(1)) {
					isonline = true;
				}
			}
			entergroupBean.setLiveId(liveid);
			entergroupBean.setUserId(uid);
			entergroupCall.setData(entergroupBean);
			entergroupCall.callService();
			Logger.verifyEquals("200", entergroupCall.getString("code"), "验证code");
			LiveRoomCallService.PublishLiveStatCall(liveid);
			verifyDB(entergroupBean, isonline, isview, bonline, bview);
			Logger.comment("验证日志数据");
			Map m = new YmtLoggingWapper().selectLiveBehaviorById(liveid, uid);
			Logger.debug(m);
			Logger.verifyEquals(1, m.get("BehaviorType"), "验证BehaviorType");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_EnterGroup_012() {
		Logger.start(true, "直播前进入直播间成功,计数想看");
		try {
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			liveid = livedto.getId();
			Integer uid = 1;
			// 删除存量记录
			liveRoomWapper.deleteLivemember(liveid, uid);
			LiveRoomCallService.PublishLiveStatCall(null);
			// 查一下当前的在线数和观看数
			Map liveroom = liveRoomWapper.selectLiveRoomStatById(liveid);
			Integer PreViewRealNum = 0;
			entergroupBean.setLiveId(liveid);
			entergroupBean.setUserId(uid);
			entergroupCall.setData(entergroupBean);
			entergroupCall.callService();
			Logger.verifyEquals("200", entergroupCall.getString("code"), "验证code");
			LiveRoomCallService.PublishLiveStatCall(liveid);
			 liveroom = liveRoomWapper.selectLiveRoomStatById(liveid);
			Integer PreViewRealNum2 = Integer.valueOf(liveroom.get("PreViewRealNum").toString());
			Logger.verifyEquals(PreViewRealNum+1,PreViewRealNum2,  "验证PreViewRealNum");
			Logger.comment("验证日志数据");
			Map m = new YmtLoggingWapper().selectLiveBehaviorById(liveid, uid);
			Logger.debug(m);
			Logger.verifyEquals(1, m.get("BehaviorType"), "验证BehaviorType");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}